<?php

// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------


namespace app\index\controller;

use app\index\BaseController;
use app\index\model\charge\ChargePileCost;
use app\index\model\charge\ChargePileGun;
use app\index\model\charge\ChargingStation;
use app\index\model\charge\PileClient;
use app\index\model\common\SystemConfig;
use app\index\model\order\OrderStatus;
use app\index\model\order\PileOrder;
use app\index\model\user\User;
use app\index\model\user\UserCard;
use app\index\model\user\UserStationCollection;
use app\worker\Events;
use Exception;
use think\facade\Db;
use think\facade\Request;
use app\index\model\charge\ChargePile as ChargePileModel;


/**
 * Class Auth
 * @package app\controller\api
 * @author xaboy
 * @day 2020-05-06
 */
class ChargePile extends BaseController
{
    /**
     * 进入充电站页面
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function stationDetails(){
        $getData = Request::post();
        [$page,$limit] = $this->getPage();
        if(!$getData['id']) return $this->fail('请选择充电站');
        $chargingStationModel = new ChargingStation();
        $stationFile = 'id,title,icon_img,longitude,latitude,max_power,station_remark,remark,parking_remark,is_open,is_delete,is_status,address,picture';
        $stationInfo = $chargingStationModel->getInfoById($getData['id'],$stationFile);
        if(!$stationInfo) return $this->fail('请选择正确的充电站');
        if($stationInfo['is_delete'] == 0) return $this->fail('请选择正确的充电站');
        if($stationInfo['is_status'] == 0) return $this->fail('当前充电站暂未启用');
        if($stationInfo['is_open'] == 0) return $this->fail('当前充电站暂未对外开放');
        $stationInfo['picture'] = json_decode($stationInfo['picture'],true);
        $webUrl = config('index.web_url');
        $pictureList = [];
        foreach ($stationInfo['picture'] as $key=>$value){
            $pictureList[] = $webUrl.$value;
        }
        $stationInfo['picture'] = $pictureList;
        $userId = $this->getLogUserId();
        if($userId){
            $userStationCollectionModel = new UserStationCollection();
            $collectionWhere = ['station_id'=>$getData['id'],'user_id'=>$userId];
            $getCollectionResult = $userStationCollectionModel->getInfoByWhere($collectionWhere);
            $stationInfo['is_collect'] = $getCollectionResult ? 1 : 0;

        }else{
            $stationInfo['is_collect'] = 0;
        }
        //获取该充电站的充电枪
        $pileGunModel = new ChargePileGun();
        $gunCount = $pileGunModel->getStationGunCount($getData['id']);
        $pileField = 'p.id as pile_id,p.device_sn,p.station_id,p.title,p.number_sn,p.device_type,p.charge_type,p.power_number,p.voltage,p.electric_current,p.charge_cost,p.service_cost,p.is_status,p.peak_electric_cost,p.peak_electric_cost,p.peak_service_cost,p.flat_electric_cost,p.flat_service_cost,p.valley_electric_cost,p.valley_service_cost,p.cost_id,p.cost_status,g.id as gun_id,g.is_status as gun_status,g.error_remark,g.num';
        $gunList = $pileGunModel->getStationGunList($getData['id'],$page,$limit,$pileField);
        $backData = ['station_info'=>$stationInfo,'gun_count'=>$gunCount,'gun_list'=>$gunList];
        return $this->success('获取成功',$backData);
    }

    /**
     * 获取充电枪信息
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function gunDetails(){
        $getData = Request::post();
        if(!$getData['gun_id']) return $this->fail('请选择充电枪');
        if(!$getData['pile_id']) return $this->fail('请选择充电桩');
        $chargePileModel = new ChargePileModel();
        $pileField = 'id,station_id,title,number_sn,device_type,charge_type,power_number,voltage,electric_current,charge_cost,service_cost,is_status,peak_electric_cost,peak_electric_cost,peak_service_cost,flat_electric_cost,flat_service_cost,valley_electric_cost,valley_service_cost,cost_id,cost_status,device_sn,remark';
        $pileInfo = $chargePileModel->getInfoById($getData['pile_id'],$pileField);
        if(!$pileInfo) return $this->fail("选择充电桩不存在");
        $deviceType = [1=>'直流',2=>'交流'];
        $chargeType = [1=>'快充',2=>'慢充'];
        $pileInfo['device_name'] = $deviceType[$pileInfo['device_type']];
        $pileInfo['charge_name'] = $chargeType[$pileInfo['charge_type']];
        $chargePileGunModel = new ChargePileGun();
        $gunWhere = ['id'=>$getData['gun_id'],'pile_id'=>$getData['pile_id']];
        $getGunInfo = $chargePileGunModel->getGunInfoByWhere($gunWhere,'id,pile_id,is_status,error_remark,num');
        if(!$getGunInfo) return $this->fail("选择充电枪不存在");
        $statusList = [0=>'离线',1=>'空闲',2=>'异常',3=>'充电'];
        $getGunInfo['status_name'] = $statusList[$getGunInfo['is_status']];
        //获取当前的价格
        $chargePileCostModel = new ChargePileCost();
        $getGunInfo['gun_cost'] = $chargePileCostModel->getPileChargeCostInfo($pileInfo);
        //获取充电桩设置的模型
        $costList = [];
        if($pileInfo['cost_id']){
            $ChargePileCostModel = new ChargePileCost();
            $getCostList = $ChargePileCostModel->getInfoById($pileInfo['cost_id']);
            if($getCostList){
                $costList = json_decode($getCostList['cost_value'],true);
                foreach ($costList as $key=>$value){
                    if($value['value'] == '01'){
                        $costList[$key]['electric_cost'] = bcmul($pileInfo['peak_electric_cost'] ,1 ,2);
                        $costList[$key]['service_cost'] = bcmul($pileInfo['peak_service_cost'] ,1 ,2);
                        $costList[$key]['value_name'] = '峰费';
                    }else if($value['value'] == '02'){
                        $costList[$key]['electric_cost'] = bcmul($pileInfo['flat_electric_cost'] ,1 ,2);
                        $costList[$key]['service_cost'] = bcmul($pileInfo['flat_service_cost'] ,1 ,2);
                        $costList[$key]['value_name'] = '平费';
                    }else if($value['value'] == '03'){
                        $costList[$key]['electric_cost'] = bcmul($pileInfo['valley_electric_cost'] ,1 ,2);
                        $costList[$key]['service_cost'] = bcmul($pileInfo['valley_service_cost'] ,1 ,2);
                        $costList[$key]['value_name'] = '谷费';
                    }else if($value['value'] == '00'){
                        $costList[$key]['electric_cost'] = bcmul($pileInfo['charge_cost'] ,1 ,2);
                        $costList[$key]['service_cost'] = bcmul($pileInfo['service_cost'] ,1 ,2);
                        $costList[$key]['value_name'] = '尖费';
                    }else{
                        $costList[$key]['electric_cost'] = '';
                        $costList[$key]['service_cost'] = '';
                    }
                }
            }
        }
        $chargingStationModel = new ChargingStation();
        $stationFile = 'id,title,icon_img,longitude,latitude,max_power,station_remark,remark,parking_remark,is_open,is_delete,is_status,address,picture';
        $stationInfo = $chargingStationModel->getInfoById($pileInfo['station_id'],$stationFile);
        $stationInfo['cost_info'] = $chargePileModel->backPileChargeConst($pileInfo);
        $userId = $this->getLogUserId();
        $stationInfo['is_charge'] = 0;
        if($userId){
            $userStationCollectionModel = new UserStationCollection();
            $collectionWhere = ['station_id'=>$pileInfo['station_id'],'user_id'=>$userId];
            $getCollectionResult = $userStationCollectionModel->getInfoByWhere($collectionWhere);
            $stationInfo['is_collect'] = $getCollectionResult ? 1 : 0;
            //判断是否有充电订单
            $pileOrderModel = new PileOrder();
            $checkInfo = $pileOrderModel->checkChargeOrder($userId);
            if($checkInfo) $stationInfo['is_charge'] = 1;
        }else{
            $stationInfo['is_collect'] = 0;
        }
        return $this->success('获取成功',['pile_info'=>$pileInfo,'gun_info'=>$getGunInfo,'cost_list'=>$costList,'station_info'=>$stationInfo]);
    }

    /**
     * 启动充电桩
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function gunFiringCharge(){
        $userId = $this->request->uid();
        //判断是否有未支付订单
        $pileOrderModel = new PileOrder();
        $checkInfo = $pileOrderModel->checkOrder($userId);
        if($checkInfo) return $this->fail('存在未支付订单，请先支付订单');
        $getData = Request::post();
        if(!$getData['gun_id']) return $this->fail('请选择充电枪');
        if(!$getData['pile_id']) return $this->fail('请选择充电桩');
        //获取当前设备是否处于链接状态
        $pileClientModel = new PileClient();
        $pileClientInfo = $pileClientModel->getInfoByPileId($getData['pile_id']);
        if(!$pileClientInfo || $pileClientInfo['is_delete'] != 1) return $this->fail('当前设备处于离线状态');
        $userModel = new User();
        $userInfo = $userModel->getUserInfoById($userId);
        if($userInfo['status'] == 0){
            return $this->fail('当前用户状态异常，请联系客服');
        }
        //默认余额支付开启
        $systemConfigModel = new SystemConfig();
        $payStatusData = $systemConfigModel->getValueByKey('default_balance_pay_status');
        if($payStatusData){
            if($userInfo['now_money'] <= 0) return $this->fail('当前账户余额不足，请先充值');
        }
        //获取充电桩
        $chargePileModel = new ChargePileModel();
        $pileInfo = $chargePileModel->getInfoById($getData['pile_id']);
        if(!$pileInfo) return $this->fail("选择充电桩不存在");
        if($pileInfo['is_status'] == 0) return $this->fail("当前充电桩暂未启用");
        if($pileInfo['cost_status'] == 0 || $pileInfo['cost_id'] == 0) return $this->fail("当前充电桩暂未设置收费模型");
        //获取充电模型
        $chargePileCostModel = new ChargePileCost();
        $costInfo = $chargePileCostModel->getInfoById($pileInfo['cost_id']);
        if($costInfo['is_status'] == 2) return $this->fail("当前充电桩暂未设置收费模型");
        //获取充电枪
        $chargePileGunModel = new ChargePileGun();
        $gunWhere = ['id'=>$getData['gun_id'],'pile_id'=>$getData['pile_id']];
        $getGunInfo = $chargePileGunModel->getGunInfoByWhere($gunWhere,'id,pile_id,is_status,error_remark,num');
        if(!$getGunInfo) return $this->fail("选择充电枪不存在");
        //充电枪空闲状态
        if($getGunInfo['is_status'] == 1){
            //获取用户卡信息
            $userCardModel = new UserCard();
            $userCardInfo = $userCardModel->getUserCardInfo($userId);
            if(!$userCardInfo){
                $cardId = $userCardModel->addUserCard($userId,['pwd'=>'888888']);
                if(!$cardId) return $this->fail("启动充电失败，请重试");
                $userCardInfo = $userCardModel->getInfoById($cardId);
                if(!$userCardInfo) return $this->fail("启动充电失败，请重试");
            }
            //用户充电卡位正常状态
            if($userCardInfo['status'] == 1){
                $orderInfo = $pileOrderModel->addOrder($userInfo,$pileInfo,$getGunInfo,$costInfo);
                if(!$orderInfo) return $this->fail("启动充电失败，请稍后重试");
                //添加操作记录
                $orderStatusModel = new OrderStatus();
                $billId = $orderStatusModel->addInfo($orderInfo,1,'用户创建充电订单');
                $sendData = ['order_info'=>$orderInfo,'pile_gun'=>$getGunInfo,'user_card'=>$userCardInfo,'user_info'=>$userInfo];
                $sentResult = Events::sendMsg($pileInfo,$pileClientInfo,$sendData,'platformStartCharge');
                //$sentResult = ['code'=>0];
                if($sentResult['code'] == 0){
                    return $this->success('正在启动充电中，请稍后……',['order_id'=>$orderInfo['id']]);
                }else{
                    $orderData = ['is_system_del'=>1,'remark'=>$sentResult['msg']];
                    $pileOrderModel->updateOrder($orderInfo['id'],$orderData);
                    return $this->fail("启动充电失败，请稍后重试");
                }
            }else{
                if($userCardInfo['status'] == 0) return $this->fail("当前充电账户已被禁止充电，请联系客服");
                if($userCardInfo['status'] == 2) return $this->fail("当前充电账户存在未结账记录，请先结账");
                if($userCardInfo['status'] == 3) return $this->fail("当前充电账户正在使用充电中");
                return $this->fail("当前用户账号异常");
            }
        }else{
            if($getGunInfo['is_status'] == 0) return $this->fail("当前充电枪处于离线状态");
            if($getGunInfo['is_status'] == 2) return $this->fail("当前充电枪处于异常状态");
            if($getGunInfo['is_status'] == 3) return $this->fail("当前充电枪处于充电状态");
            return $this->fail("当前充电枪处于异常状态");
        }
    }

    /**
     * 扫码启动充电枪
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function codeFiringCharge(){
        $userId = $this->request->uid();
        //获取用户信息
        $userModel = new User();
        $userInfo = $userModel->getUserInfoById($userId);
        if($userInfo['status'] == 0){
            return $this->fail('当前用户状态异常，请联系客服',4);
        }
        //默认余额支付开启
        $systemConfigModel = new SystemConfig();
        $payStatusData = $systemConfigModel->getValueByKey('default_balance_pay_status');
        if($payStatusData){
            if($userInfo['now_money'] <= 0) return $this->fail('当前账户余额不足，请先充值',3);
        }
        //判断是否有未支付订单
        $pileOrderModel = new PileOrder();
        $checkInfo = $pileOrderModel->checkOrder($userId);
        if($checkInfo) return $this->fail('存在未支付订单，请先支付订单',2);
        $getData = Request::post();
        if(!$getData['pile_number']) return $this->fail('请选择充电桩');
        /*
        $getPileData = explode("-",$getData['pile_number']);
        $pileNumber = $getPileData[0];
        if(isset($getPileData[1]) && $getPileData[1]){
            $pileGunNumber = $getPileData[1];
        }else{
            $pileGunNumber = 0;
        }
        */
        $pileNumber = substr($getData['pile_number'], 0,14);//获取设备编号
        $gunNUmber =  substr($getData['pile_number'], 14,2);//获取充电桩编号
        if($gunNUmber){
            $pileGunNumber = $gunNUmber;
        }else{
            $pileGunNumber = 0;
        }
        //获取充电桩
        $chargePileModel = new ChargePileModel();
        $pileInfo = $chargePileModel->getInfoByWhere(['device_sn'=>$pileNumber]);
        if(!$pileInfo) return $this->fail("选择充电桩不存在");
        if($pileInfo['is_status'] == 0) return $this->fail("当前充电桩暂未启用");
        if($pileInfo['cost_status'] == 0 || $pileInfo['cost_id'] == 0) return $this->fail("当前充电桩暂未设置收费模型");
        //获取当前设备是否处于链接状态
        $pileClientModel = new PileClient();
        $pileClientInfo = $pileClientModel->getInfoByPileId($pileInfo['id']);
        if(!$pileClientInfo || $pileClientInfo['is_delete'] != 1) return $this->fail('当前设备处于离线状态');
        //获取充电枪
        $chargePileGunModel = new ChargePileGun();
        if($pileGunNumber){
            $gunWhere = ['num'=>$pileGunNumber,'pile_id'=>$pileInfo['id']];
            $getGunInfo = $chargePileGunModel->getGunInfoByWhere($gunWhere,'id,pile_id,is_status,error_remark,num');
        }else{
            $gunWhere = ['pile_id'=>$pileInfo['id']];
            $getGunInfo = $chargePileGunModel->getGunInfoByWhere($gunWhere,'id,pile_id,is_status,error_remark,num');
        }
        if(!$getGunInfo) return $this->fail("选择充电枪不存在");
        //判断当前充电桩是否有在启动中的订单
        $orderWhere = [
            'gun_id'=>$getGunInfo['id'],
            'pile_id'=>$pileInfo['id'],
            'is_del'=>0,
            'is_system_del'=>0,
            'status'=>0,
        ];
        $checkOrderInfo = $pileOrderModel->getInfoByWhere($orderWhere);
        if($checkOrderInfo){
            //在60秒内
            if(strtotime($checkOrderInfo['create_time']) > (time() - 120)){
                return $this->success('正在启动充电中，请稍后……',['order_id'=>$checkOrderInfo['id']]);
            }
            $saveOrderData = ['is_system_del'=>1,'update_time'=>date("Y-m-d H:i:s")];
            $orderResult = $pileOrderModel->where($orderWhere)->update($saveOrderData);
            if(!$orderResult) return $this->fail("启动充电失败，请重试，");
        }
        //获取充电模型
        $chargePileCostModel = new ChargePileCost();
        $costInfo = $chargePileCostModel->getInfoById($pileInfo['cost_id']);
        if($costInfo['is_status'] == 2) return $this->fail("当前充电桩暂未设置收费模型");
        //充电枪空闲状态
        if($getGunInfo['is_status'] == 1){
            //获取用户卡信息
            $userCardModel = new UserCard();
            $userCardInfo = $userCardModel->getUserCardInfo($userId);
            if(!$userCardInfo){
                $cardId = $userCardModel->addUserCard($userId,['pwd'=>'888888']);
                if(!$cardId) return $this->fail("启动充电失败，请重试");
                $userCardInfo = $userCardModel->getInfoById($cardId);
                if(!$userCardInfo) return $this->fail("启动充电失败，请重试");
            }
            //用户充电卡位正常状态
            if($userCardInfo['status'] == 1){
                $orderInfo = $pileOrderModel->addOrder($userInfo,$pileInfo,$getGunInfo,$costInfo);
                if(!$orderInfo) return $this->fail("启动充电失败，请稍后重试");
                //添加操作记录
                $orderStatusModel = new OrderStatus();
                $billId = $orderStatusModel->addInfo($orderInfo,1,'用户创建充电订单');
                $sendData = ['order_info'=>$orderInfo,'pile_gun'=>$getGunInfo,'user_card'=>$userCardInfo,'user_info'=>$userInfo];
                $sentResult = Events::sendMsg($pileInfo,$pileClientInfo,$sendData,'platformStartCharge');
                //$sentResult = ['code'=>0];
                if($sentResult['code'] == 0){
                    return $this->success('正在启动充电中，请稍后……',['order_id'=>$orderInfo['id']]);
                }else{
                    $orderData = ['is_system_del'=>1,'remark'=>$sentResult['msg']];
                    $pileOrderModel->updateOrder($orderInfo['id'],$orderData);
                    return $this->fail("启动充电失败，请稍后重试");
                }
            }else{
                if($userCardInfo['status'] == 0) return $this->fail("当前充电账户已被禁止充电，请联系客服");
                if($userCardInfo['status'] == 2) return $this->fail("当前充电账户存在未结账记录，请先结账");
                if($userCardInfo['status'] == 3) return $this->fail("当前充电账户正在使用充电中");
                return $this->fail("当前用户账号异常");
            }
        }else{
            if($getGunInfo['is_status'] == 0) return $this->fail("当前充电枪处于离线状态");
            if($getGunInfo['is_status'] == 2) return $this->fail("当前充电枪处于异常状态");
            if($getGunInfo['is_status'] == 3) return $this->fail("当前充电枪处于充电状态");
            return $this->fail("当前充电枪处于异常状态");
        }
    }

    /**
     * 获取充电站详情
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getStationByGun(){
        $getData = Request::post();
        if(!$getData['pile_number']) return $this->fail('请选择充电桩');
        /*
        $getPileData = explode("-",$getData['pile_number']);
        $pileNumber = $getPileData[0];
        */
        $pileNumber = substr($getData['pile_number'], 0,14);//获取设备编号
        $gunNUmber =  substr($getData['pile_number'], 14,2);//获取充电桩编号
        if($gunNUmber){
            $pileGunNumber = $gunNUmber;
        }else{
            $pileGunNumber = 0;
        }
        
        //获取充电桩
        $chargePileModel = new ChargePileModel();
        $pileInfo = $chargePileModel->getInfoByWhere(['device_sn'=>$pileNumber]);
        if(!$pileInfo) return $this->fail("选择充电桩不存在");
        if(!$pileInfo['station_id']) return $this->fail("选择充电桩错误");
        //获取充电枪
        $chargePileGunModel = new ChargePileGun();
        if($pileGunNumber){
            $gunWhere = ['num'=>$pileGunNumber,'pile_id'=>$pileInfo['id']];
            $getGunInfo = $chargePileGunModel->getGunInfoByWhere($gunWhere,'id,pile_id,is_status,error_remark,num');
        }else{
            $gunWhere = ['pile_id'=>$pileInfo['id']];
            $getGunInfo = $chargePileGunModel->getGunInfoByWhere($gunWhere,'id,pile_id,is_status,error_remark,num');
        }
        if(!$getGunInfo) return $this->fail("选择充电枪不存在");
        $getData['id'] = $pileInfo['station_id'];
        $chargingStationModel = new ChargingStation();
        $stationFile = 'id,title,icon_img,longitude,latitude,max_power,station_remark,remark,parking_remark,is_open,is_delete,is_status,address,picture';
        $stationInfo = $chargingStationModel->getInfoById($getData['id'],$stationFile);
        if(!$stationInfo) return $this->fail('当前充电站未启用');
        if($stationInfo['is_delete'] == 0) return $this->fail('当前充电站未启用');
        if($stationInfo['is_status'] == 0) return $this->fail('当前充电站暂未启用');
        if($stationInfo['is_open'] == 0) return $this->fail('当前充电站暂未对外开放');
        $stationInfo['picture'] = json_decode($stationInfo['picture'],true);
        $webUrl = config('index.web_url');
        $pictureList = [];
        foreach ($stationInfo['picture'] as $key=>$value){
            $pictureList[] = $webUrl.$value;
        }
        $stationInfo['picture'] = $pictureList;
        $userId = $this->getLogUserId();
        $stationInfo['is_charge'] = 0;
        if($userId){
            $userStationCollectionModel = new UserStationCollection();
            $collectionWhere = ['station_id'=>$getData['id'],'user_id'=>$userId];
            $getCollectionResult = $userStationCollectionModel->getInfoByWhere($collectionWhere);
            $stationInfo['is_collect'] = $getCollectionResult ? 1 : 0;
            //判断是否有充电订单
            $pileOrderModel = new PileOrder();
            $checkInfo = $pileOrderModel->checkChargeOrder($userId);
            if($checkInfo) $stationInfo['is_charge'] = 1;
        }else{
            $stationInfo['is_collect'] = 0;
        }
        $stationInfo['cost_info'] = $chargePileModel->backPileChargeConst($pileInfo);
        $stationInfo['pile_id'] = $pileInfo['id'];
        $stationInfo['gun_id'] = $getGunInfo['id'];
        $stationInfo['pile_info'] = $pileInfo;
        $backData = ['station_info'=>$stationInfo];
        return $this->success('获取成功',$backData);
    }

    /**
     * 删除充电桩未启动成功的订单
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function deleteFiring(){
        $getData = Request::post();
        if(!$getData['order_id']) return $this->success('请选择充电记录');
        $pileOrderModel = new PileOrder();
        $orderInfo = $pileOrderModel->getInfoById($getData['order_id']);
        if(!$orderInfo) return $this->success('订单信息不存在');
        if($orderInfo['status'] == 0){
            if(strtotime($orderInfo['create_time']) > (time() - 120)){
                return $this->fail('充电桩正在启动中');
            }else{
                $saveOrderData = ['is_system_del'=>1,'update_time'=>date("Y-m-d H:i:s")];
                $updateResult = $pileOrderModel->updateOrder($orderInfo['id'],$saveOrderData);
                if($updateResult){
                    return $this->success('设置带充电记录成功');
                }else{
                    return $this->fail('充电桩正在启动中');
                }
            }
        }else{
            return $this->success('订单不是待充电状态');
        }
    }

    /**
     * 获取充电桩详情
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getPileInfo(){
        $getData = Request::post();
        if(!$getData['pile_id']) return $this->fail('请选择要查看的充电桩');
        $pileModel = new ChargePileModel();
        $stationModel = new ChargingStation();
        $gunModel = new ChargePileGun();
        $field = 'id,title,device_sn,station_id,device_type,charge_type,power_number,voltage,electric_current,is_status';

        $pileInfo = $pileModel->getInfoByWhere(['id'=>$getData['pile_id']],$field);
        if(!$pileInfo) return $this->fail('请选择正确的充电桩');
        $statusType = [0=>'否',1=>'是'];
        $deviceType = [1=>'直流',2=>'交流'];
        $chargeType = [1=>'快充',2=>'慢充'];
        $pileInfo['status_name'] = $statusType[$pileInfo['is_status']];
        $pileInfo['device_name'] = isset($deviceType[$pileInfo['device_type']]) ? $deviceType[$pileInfo['device_type']] : '';
        $pileInfo['charge_name'] = isset($chargeType[$pileInfo['charge_type']]) ? $chargeType[$pileInfo['charge_type']] : '';
        $pileInfo['station_name'] = $stationModel->where('id',$pileInfo['station_id'])->value('title');
        $gunInfo = $gunModel->getPileGun($pileInfo['id']);
        $pileInfo['gun_info'] = $gunInfo;
        return $this->success('获取成功',$pileInfo);
    }

}
